package com.sxkawzp.aspect;

import com.sxkawzp.anno.DataScope;
import com.sxkawzp.util.DataScopeHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author suxingkang
 * @version 1.0
 * @since 2025/1/17 14:57
 */
@Slf4j
@Aspect
@Component
public class DataScopeAspect {


    @Pointcut("@annotation(com.sxkawzp.anno.DataScope)")
    public void queryScope() {
    }

    @Around("queryScope()")
    public Object before(ProceedingJoinPoint point) throws Throwable {
        try {
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            DataScope annotation = method.getAnnotation(DataScope.class);
            DataScopeHolder.add(annotation);
            return point.proceed();
        } catch (Throwable throwable) {
            log.error("权限执行异常：", throwable);
            throw throwable;
        } finally {
            DataScopeHolder.clear();
            DataScopeHolder.clearParam();
        }
    }

}
